<?php

set_include_path(implode(PATH_SEPARATOR,
    array(
        get_include_path(),
        '..',
        dirname(dirname(dirname(__FILE__)))
    )));

require_once 'components/utils/debug_utils.php';

Warning::BeginSuppress(PHPWarning::$Deprecated);
Warning::BeginSuppress(PHPWarning::$Strict);

require_once 'tests/simpletest/autorun.php';
require_once 'database_engine/commands.php';

class SelectCommandBuilderTests extends UnitTestCase
{
    public function testSimpleCommandFromQuery()
    {
        $builder = new SelectCommandBuilder();
        $builder->AddSourceQuery('SELECT id FROM test_table');
        $this->assertEqual('SELECT id FROM test_table', $builder->GetSql());
    }

    public function testSimpleCommandFromTable()
    {
        $builder = new SelectCommandBuilder();
        $builder->AddSourceTable('test_table');
        $builder->AddField('id');
        $builder->AddField('value');

        $this->assertEqual('SELECT id, value FROM test_table', $builder->GetSql());
    }

    public function testSimpleCommandWithCondition()
    {
        $builder = new SelectCommandBuilder();
        $builder->AddSourceTable('test_table');
        $builder->AddField('id');
        $builder->AddField('value');
        $builder->AddCondition(new SMCondition('id', ConditionOperator::$Equals, 1));

        $this->assertEqual('SELECT id, value FROM test_table WHERE (id = 1)', $builder->GetSql());
    }

    public function testSimpleAliasedTable()
    {
        $builder = new SelectCommandBuilder();
        $builder->AddSourceTable('test_table', 't');
        $builder->AddField('id', 't');
        $builder->AddField('value', 't');

        $this->assertEqual('SELECT t.id, t.value FROM test_table t', $builder->GetSql());
    }

    public function testSimpleAliasedField()
    {
        $builder = new SelectCommandBuilder();
        $builder->AddSourceTable('test_table', 't');
        $builder->AddField('id', 't', 'id_a');
        $builder->AddField('value', null, 'value_a');

        $this->assertEqual('SELECT t.id AS id_a, value AS value_a FROM test_table t', $builder->GetSql());
    }

    public function testAddSimpleJoin()
    {
        $builder = new SelectCommandBuilder();
        $builder->AddSourceTable('test_table', 't');
        $builder->AddField('id', 't');
        $builder->AddField('value', 't');

        $builder->AddJoinTable(JoinType::$Inner, 'test_table2', 't2',
            new SMCondition(
                $builder->CreateFieldInfo('id', 't'),
                ConditionOperator::$Equals,
                $builder->CreateFieldInfo('link_id', 't2')
            )
        );

        $this->assertEqual('SELECT t.id, t.value FROM test_table t INNER JOIN test_table2 t2 ON t.id = t2.link_id', $builder->GetSql());
    }
}

class ConditionTests extends UnitTestCase
{
    public function testSimpleCondition()
    {
        $condition = new SMCondition('id', ConditionOperator::$Equals, 1);
        $this->assertEqual('id = 1', $condition->GetAsSql());
    }

    public function testConditionOnString()
    {
        $condition = new SMCondition('value', ConditionOperator::$Equals, '\'string value\'');
        $this->assertEqual('value = \'string value\'', $condition->GetAsSql());
    }
    
}


?>
